package com.yonyou.ucf.mdf.app.isvdxq.utils;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.Normalizer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MdfLog {
    private static final List<String> NORMALIZE_LIST = Arrays.asList("\r", "\n");
    private static final Logger logger = LoggerFactory.getLogger(MdfLog.class);
    private static final int LOGGER_THREADS = 4;
    private static final String FORMAT_THREAD_LOGGER = "thread-logger-%d";
    private static final ThreadPoolExecutor loggerPool;

    public MdfLog() {
    }

    public static void debug(String msg, Object... args) {
        LoggerThread loggerThread = new LoggerThread("debug", msg, args);
        loggerPool.execute(loggerThread);
    }

    public static void info(String msg, Object... args) {
        LoggerThread loggerThread = new LoggerThread("info", msg, args);
        loggerPool.execute(loggerThread);
    }

    public static void warn(String msg, Object... args) {
        LoggerThread loggerThread = new LoggerThread("warn", msg, args);
        loggerPool.execute(loggerThread);
    }

    public static void error(String msg, Object... args) {
        LoggerThread loggerThread = new LoggerThread("error", msg, args);
        loggerPool.execute(loggerThread);
    }

    public static void logger(String type, String msg, Object... args) {
        if ("debug".equals(type)) {
            if (logger.isDebugEnabled()) {
                logger.debug(msg, parseLogInfo(args));
            }
        } else if ("info".equals(type)) {
            if (logger.isInfoEnabled()) {
                logger.info(msg, parseLogInfo(args));
            }
        } else if ("warn".equals(type)) {
            if (logger.isWarnEnabled()) {
                logger.warn(msg, parseLogInfo(args));
            }
        } else if ("error".equals(type) && logger.isErrorEnabled()) {
            logger.error(msg, parseLogInfo(args));
        }

    }

    private static Object[] parseLogInfo(Object... args) {
        if (args != null && args.length != 0) {
            Object[] parseArgs = new Object[args.length];

            for(int i = 0; i < args.length; ++i) {
                parseArgs[i] = args[i];
                if (parseArgs[i] instanceof String) {
                    String argStr = (String)parseArgs[i];
                    String normalize = Normalizer.normalize(argStr, Normalizer.Form.NFKC);

                    String str;
                    for(Iterator var5 = NORMALIZE_LIST.iterator(); var5.hasNext(); normalize = normalize.replace(str, "")) {
                        str = (String)var5.next();
                    }

                    parseArgs[i] = normalize;
                }
            }

            return parseArgs;
        } else {
            return args;
        }
    }

    static {
        loggerPool = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), (new BasicThreadFactory.Builder()).namingPattern("thread-logger-%d").build());
    }
}

